package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.dto.BaseBookDto;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.queryBean.BaseBookQueryBean;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.vo.BusinessAreaVo;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BaseAllBooks implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
        try {
            BaseBookQueryBean bean = JsonUtils.objectToPojo(req.getData(), BaseBookQueryBean.class);
            BaseBookMapper mapper = factory.getMapper(BaseBookMapper.class);
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
            List<String> areaIdList = Tools.queryChildrenArea(bean.getBusinessArea(),userInfo.getCurWaterCode(),businessAreaMapper);
            bean.setBusinessAreaList(areaIdList);
            List<BaseBookListVO> list = mapper.select(bean);
            List<BusinessAreaVo> businessAreaBeans = businessAreaMapper.selectList(userInfo.getCurWaterCode());
            Map<String,String> businessMap = businessAreaBeans.stream().collect(Collectors.toMap(BusinessAreaVo::getAreaNo,BusinessAreaVo::getAreaName));
            List<BaseBookDto> baseBookDto = new ArrayList<>();
            if (list != null && list.size()>0){
                String meterReadingStaffName = list.get(0).getMeterReadingStaffName()==null?"":list.get(0).getMeterReadingStaffName();
                List<BaseBookListVO> listVOS = new ArrayList<>();
                BaseBookDto bookDto = new BaseBookDto();
                for (BaseBookListVO baseBookListVO: list) {
                    if (meterReadingStaffName == null || meterReadingStaffName.equals(baseBookListVO.getMeterReadingStaffName())) {
                        bookDto.setAreaStaffName(businessMap.get(baseBookListVO.getBusinessArea()) + "-" + baseBookListVO.getMeterReadingStaffName());
                        listVOS.add(baseBookListVO);
                    } else {
                        if (listVOS.size() > 0) {
                            bookDto.setBaseBookListVOList(listVOS);
                            baseBookDto.add(bookDto);
                            listVOS = new ArrayList<>();
                            bookDto = new BaseBookDto();
                            meterReadingStaffName = baseBookListVO.getMeterReadingStaffName();
                            bookDto.setAreaStaffName(businessMap.get(baseBookListVO.getBusinessArea()) + "-" + baseBookListVO.getMeterReadingStaffName());
                            listVOS.add(baseBookListVO);
                        }
                    }
                }
                if (listVOS.size()>0){
                    bookDto.setBaseBookListVOList(listVOS);
                    baseBookDto.add(bookDto);
                }
            }
            info.setData(baseBookDto);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription("操作失败");
            logger.error("操作失败", e);
        }
        return info;
    }
}
